package com.more.offer.algorithm.leetcode.lcode20;

import java.util.*;
import java.util.jar.JarOutputStream;

/**
 * @author pinoc
 * @date 2021/8/14
 * @description
 */
public class Solution {
	private static Map<Character, Character> pairs = new HashMap<Character, Character>() {{
		put(')', '(');
		put(']', '[');
		put('}', '{');
	}};
	public static void main(String[] args) {
		Solution solution = new Solution();
		boolean b = solution.isValid("((([{}])))");
		System.out.println(b);
	}
	public boolean isValid(String str) {
		Stack<Character> stack = new Stack<>();
		for (int i = 0; i < str.length(); i++) {
			char c = str.charAt(i);
			if(pairs.containsKey(c)) {
				if(stack.isEmpty() || !stack.pop().equals(pairs.get(c))) {
					return false;
				}
			} else {
				stack.push(c);
			}
		}
		return stack.isEmpty();
	}
}
